#include "connectmysql.h"

#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>

ConnectMySQL::ConnectMySQL()
{

}

bool ConnectMySQL::createConnection()
{
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QMYSQL");
    /**正式数据库*/
    db.setHostName("172.10.70.110");
    db.setDatabaseName("sc_mes");
    db.setUserName("baixin");
    db.setPassword("100Trust!@");

//    db.setHostName("172.10.70.240");
//    db.setDatabaseName("db_mes");
//    db.setUserName("root");
//    db.setPassword("200trust");
    db.setConnectOptions("MYSQL_OPT_CONNECT_TIMEOUT=10;MYSQL_OPT_READ_TIMEOUT=10;MYSQL_OPT_WRITE_TIMEOUT=10");
    if(!db.open())
    {
        qCritical("Can't open database:%s(%s)",
                  db.lastError().text().toLocal8Bit().data(),
                  qt_error_string().toLocal8Bit().data());
        return false;
    }
    return true;
}

void ConnectMySQL::closeConnection()
{
    QSqlDatabase::database().close();
}
